package com.dlwx.firefighting.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.recyclerview.ProgressStyle;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.dlwx.firefighting.R;
import com.dlwx.firefighting.adapter.TransferListCashierAdapter;
import com.dlwx.firefighting.bean.CashierHisBean;
import com.dlwx.firefighting.config.Constant;
import com.dlwx.firefighting.utils.SPUtils;
import com.dlwx.firefighting.utils.ToastUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CashierHisActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.transferlist_recycler)
    LRecyclerView transferlistRecycler;
    private LRecyclerView recyclerView;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private TransferListCashierAdapter mAdapter;
    private int page=1;
    private int requestNum=10;
    private int count=0;
    private String userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cashier_his);
        ButterKnife.bind(this);
        userId= (String) SPUtils.get(this, Constant.USER_ID,"");
        initView();
        initRecyView();
    }

    @OnClick({R.id.title_left_one_btn, R.id.tv_middle_title})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;
            case R.id.tv_middle_title:
                break;
        }
    }


    private void getDatas() {
        OkGo.<String>post(Constant.TCASHIER_HIS)
                .tag(this)
                .params("user_id", userId)
                .params("page", page)
                .params("num", requestNum)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String res=response.body().toString().trim();
                        if(TextUtils.isEmpty(res)){
                            ToastUtils.toastForShort(CashierHisActivity.this,getString(R.string.empty_datas));
                            return ;
                        }
                        try {
                            CashierHisBean bean= new Gson().fromJson(res,new TypeToken<CashierHisBean>() {}.getType());
                            if(bean.getStatus()== Constant.SUCESS){
                                List<CashierHisBean.DataBean.AccountLogBean> beans = bean.getData().getAccount_log();
                                if(beans.isEmpty()){
                                    ToastUtils.toastForShort(CashierHisActivity.this,getString(R.string.empty_datas));
                                    return;
                                }
                                setDatas(beans);
                            }else{
                                ToastUtils.toastForShort(CashierHisActivity.this,bean.getMsg());

                            }
                        }catch (Exception e){
                            ToastUtils.toastForShort(CashierHisActivity.this,getString(R.string.analysis_error));
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        ToastUtils.toastForShort(CashierHisActivity.this, getString(R.string.bad_net));
                        super.onError(response);
                    }
                });
    }

    private void setDatas(List<CashierHisBean.DataBean.AccountLogBean> beans) {
       /* if(beans.isEmpty()){
            return;
        }*/
        mAdapter.addAll(beans);
        count=mAdapter.getItemCount();
        recyclerView.refreshComplete(requestNum);
    }

    @Override
    protected void onResume() {
        super.onResume();
        refresh();
    }

    private void initView() {
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText("转账提现记录");
    }

    /**
     * 初始化recyclerview
     */
    private void initRecyView() {
        recyclerView = (LRecyclerView) findViewById(R.id.transferlist_recycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new TransferListCashierAdapter(CashierHisActivity.this);
        lRecyclerViewAdapter = new LRecyclerViewAdapter(mAdapter);
        recyclerView.setAdapter(lRecyclerViewAdapter);

        DividerDecoration divider = new DividerDecoration.Builder(this)
                .setHeight(R.dimen.divider_height)
                .setPadding(R.dimen.divider_padding)
                .setColorResource(R.color.line_gray)
                .build();

        recyclerView.setHasFixedSize(true);
        recyclerView.addItemDecoration(divider);

        recyclerView.setRefreshProgressStyle(ProgressStyle.LineSpinFadeLoader);
        recyclerView.setArrowImageView(R.drawable.ic_pulltorefresh_arrow);
        recyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallSpinFadeLoader);

        //设置下拉刷新
        recyclerView.setPullRefreshEnabled(true);
        recyclerView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();

            }
        });
        //设置上拉加载更多
        recyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                Log.d("CCC",count+"---"+page);
                if(count<(page*requestNum)){
                    ToastUtils.toastForShort(CashierHisActivity.this,getString(R.string.no_more));
                    recyclerView.setNoMore(true);
                }else{
                    page++;
                    getDatas();

                }
            }
        });
        //设置头部加载颜色
        recyclerView.setHeaderViewColor(R.color.colorAccent, R.color.black, android.R.color.white);
        //设置底部加载颜色
        recyclerView.setFooterViewColor(R.color.colorAccent, R.color.black, android.R.color.white);
        //设置底部加载文字提示
        recyclerView.setFooterViewHint("拼命加载中", "已经全部为你呈现了", "网络不给力啊，点击再试一次吧");
        //初始化数据

    }
    /**
     * 刷新
     */
    private void refresh() {
        mAdapter.clear();
        lRecyclerViewAdapter.notifyDataSetChanged();
        page=1;
        getDatas();
    }


}
